package com.hansen.library.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 时间util
 * Created by han on 2017/6/1 0001.
 */
public class TimeUtil {
    private static SimpleDateFormat sdf = null;

    private static StringBuilder stringBuilder;

    public static String formatUTC(long l, String strPattern) {
        if (TextUtils.isEmpty(strPattern)) {
            strPattern = "yyyy-MM-dd HH:mm:ss";
        }
        if (sdf == null) {
            try {
                sdf = new SimpleDateFormat(strPattern, Locale.CHINA);
            } catch (Throwable e) {
            }
        } else {
            sdf.applyPattern(strPattern);
        }
        return sdf == null ? "NULL" : sdf.format(l);
    }

    /**
     * 将毫秒转化成固定格式的时间
     * 时间格式: yyyy-MM-dd-HHmmssSSS
     *
     * @param millisecond 时间毫秒
     * @return 时间格式: yyyy-MM-dd-HHmmssSSS
     */
    public static String getDateTimeFromMillisecond(Long millisecond) {
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HHmmssSSS");
        Date date = new Date(millisecond);
        return simpleDateFormat.format(date);
    }

    // 字符串转指定格式时间到day
    public static String getDateToDay(String str) {
        if (StringUtils.isEmpty(str)) {
            return "";
        }
        return StringToDate(str, "yyyy-MM-dd");
    }

    // 字符串转指定格式时间到minute
    public static String getDateWithMinute(String str) {
        if (StringUtils.isEmpty(str)) {
            return "";
        }
        return StringToDate(str, "yyyy-MM-dd HH:mm");
    }

    // 字符串转指定格式时间到second
    public static String getDateWithSecond(String str) {
        if (StringUtils.isEmpty(str)) {
            return "";
        }
        return StringToDate(str, "yyyy-MM-dd HH:mm:ss");
    }

    // 字符串转指定格式时间只有月-天-小时
    public static String getDateNoYear(String str) {
        if (StringUtils.isEmpty(str)) {
            return "";
        }

        String formatStr = "yyyy-MM-dd HH:mm";

        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        Calendar calender = Calendar.getInstance();

        try {
            calender.setTime(sdf.parse(str));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String month = (calender.get(Calendar.MONTH) + 1) < 10 ? ("0" + (calender.get(Calendar.MONTH) + 1)) : String.valueOf((calender.get(Calendar.MONTH) + 1));
        String day = (calender.get(Calendar.DAY_OF_MONTH) < 10) ? ("0" + calender.get(Calendar.DAY_OF_MONTH)) : String.valueOf(calender.get(Calendar.DAY_OF_MONTH));
        String hour = (calender.get(Calendar.HOUR_OF_DAY) < 10) ? ("0" + calender.get(Calendar.HOUR_OF_DAY)) : String.valueOf(calender.get(Calendar.HOUR_OF_DAY));
        String minute = (calender.get(Calendar.MINUTE) < 10) ? ("0" + calender.get(Calendar.MINUTE)) : String.valueOf(calender.get(Calendar.MINUTE));

        return month + "-" + day + " " + hour + ":" + minute;
    }

    // 字符串转指定格式时间到minute
    public static String getDateOnlyDay(String str) {
        if (StringUtils.isEmpty(str)) {
            return "";
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calender = Calendar.getInstance();

        try {
            calender.setTime(sdf.parse(str));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return String.valueOf(calender.get(Calendar.DAY_OF_MONTH));
    }

    public static String StringToDate(String dateStr, String formatStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        Date date = null;

        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        SimpleDateFormat s = new SimpleDateFormat(formatStr);

        return s.format(date);
    }

    /**
     * 时间的处理
     *
     * @param time
     * @return
     */
    public static String getTimeFromInt(long time) {
        if (time <= 0) {
            return "0:00";
        }
        long secondnd = (time / 1000) / 60;
        long million = (time / 1000) % 60;
        String f = String.valueOf(secondnd);
        String m = million >= 10 ? String.valueOf(million) : "0" + String.valueOf(million);

        return f + ":" + m;
    }

    /**
     * 毫秒转化为分钟和秒
     *
     * @param duration
     * @return
     */
    public static String timeParse(long duration) {

        stringBuilder = new StringBuilder();

        long minute = duration / 60000;
        long seconds = duration % 60000;

        long second = seconds / 1000;

        if (minute < 10) {
            stringBuilder.append("0");
        }

        stringBuilder.append(minute);
        stringBuilder.append(":");

        if (second < 10) {
            stringBuilder.append("0");
        }

        stringBuilder.append(second);

        return stringBuilder.toString();
    }

    public static String getMDByDateString(String dateStr) {
        if (StringUtils.isEmpty(dateStr)) {
            return "";
        }

        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        Date date = null;

        try {
            date = formatter.parse(dateStr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            LogUtils.e(e.toString());
        }

        GregorianCalendar calendar = new GregorianCalendar();

        calendar.setTime(date);

        return calendar.get(Calendar.MONTH) + 1 + "月" + calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static String getYMDHmsTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String getYMDHmTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    public static String getYMDTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    //获取当前时间
    public static String getCurrentTimeMillis() {
        return String.valueOf(getCurrentLongTimeMillis());
    }


    //获取当前时间
    public static long getCurrentLongTimeMillis() {
        return System.currentTimeMillis();
    }

    //获取当前时间
    public static String getCurrentTimeYMD() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        Date curDate = new Date(System.currentTimeMillis());

        return formatter.format(curDate);
    }

    //获取当前时间
    public static String getCurrentTimeYM() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");

        Date curDate = new Date(System.currentTimeMillis());

        return formatter.format(curDate);
    }

    public static boolean isToday(Date date) {
        if (date == null) {
            return false;
        }

        Calendar current = Calendar.getInstance();

        Calendar today = Calendar.getInstance();
        today.set(Calendar.YEAR, current.get(Calendar.YEAR));
        today.set(Calendar.MONTH, current.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);

        Calendar tomorrow = Calendar.getInstance();
        tomorrow.set(Calendar.YEAR, current.get(Calendar.YEAR));
        tomorrow.set(Calendar.MONTH, current.get(Calendar.MONTH));
        tomorrow.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH) + 1);
        tomorrow.set(Calendar.HOUR_OF_DAY, 0);
        tomorrow.set(Calendar.MINUTE, 0);
        tomorrow.set(Calendar.SECOND, 0);

        current.setTime(date);

        return current.after(today) && current.before(tomorrow);
    }

    public static int getCurrentMonthLastDay() {
        Calendar a = Calendar.getInstance();

        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天

        int maxDate = a.get(Calendar.DATE);

        return maxDate;
    }

    //获取当前年月
    public static String getCurrentYearAndMonth() {
        Calendar a = Calendar.getInstance();

        int year = a.get(Calendar.YEAR);
        int month = a.get(Calendar.MONTH) + 1;

        return year + "年" + month + "月";
    }


    public static int getFirstDayOfMonth() {
        Calendar a = Calendar.getInstance();

        a.set(Calendar.DAY_OF_MONTH, 1); //设置为当前月第一天

        /*Calendar.DAY_OF_WEEK
         *星期日:Calendar.SUNDAY=1
         *星期一:Calendar.MONDAY=2
         *星期二:Calendar.TUESDAY=3
         *星期三:Calendar.WEDNESDAY=4
         *星期四:Calendar.THURSDAY=5
         *星期五:Calendar.FRIDAY=6
         *星期六:Calendar.SATURDAY=7
         **/
        int i = a.get(Calendar.DAY_OF_WEEK);//获取当前月第一天是周几， 周六返回字段是7，然后7-1就是周六

        return i;
    }

    /**
     * 与当前时间对比
     *
     * @param longSecond
     * @param targetIsNowTime 当前时间是否作为目标
     * @return
     */
    public static boolean compareNowTimeWithSecond(String longSecond, boolean targetIsNowTime) {
        if (!StringUtils.isIntNumber(longSecond)) {
            return false;
        }

        return targetIsNowTime ? compareLongTimeMillis(Long.parseLong(longSecond) * 1000, getCurrentLongTimeMillis())
                : compareLongTimeMillis(getCurrentLongTimeMillis(), Long.parseLong(longSecond) * 1000);
    }

    /**
     * 时间戳对比
     *
     * @param currentMillis
     * @param targetLongMillis
     * @return
     */
    public static boolean compareLongTimeMillis(long currentMillis, String targetLongMillis) {
        if (!StringUtils.isIntNumber(targetLongMillis)) {
            return false;
        }

        return compareLongTimeMillis(currentMillis, Long.parseLong(targetLongMillis));
    }

    /**
     * 时间戳对比
     *
     * @param currentMillis
     * @param targetLongMillis
     * @return
     */
    public static boolean compareLongTimeMillis(String currentMillis, long targetLongMillis) {
        if (!StringUtils.isIntNumber(currentMillis)) {
            return false;
        }

        return compareLongTimeMillis(Long.parseLong(currentMillis), targetLongMillis);
    }

    /**
     * 时间戳对比
     *
     * @param currentMillis
     * @param targetLongMillis
     * @return
     */
    public static boolean compareLongTimeMillis(String currentMillis, String targetLongMillis) {
        if (!StringUtils.isIntNumber(currentMillis) || !StringUtils.isIntNumber(targetLongMillis)) {
            return false;
        }

        return compareLongTimeMillis(Long.parseLong(currentMillis), Long.parseLong(targetLongMillis));
    }

    /**
     * 时间戳对比
     *
     * @param currentMillis
     * @param targetLongMillis
     * @return
     */
    public static boolean compareLongTimeMillis(long currentMillis, long targetLongMillis) {
        return currentMillis > targetLongMillis;
    }
}
